import 'package:flutter/material.dart';
import 'package:flutter_color_plugin/flutter_color_plugin.dart';
import 'package:flutter_learn/less_group_page.dart';
import 'package:flutter_learn/statefull_group_page.dart';

import 'layout_demo_page.dart';

void main() => runApp(MyApp());
//void main() => runApp(LessGroupPage());
//有状态组件
//void main() => runApp(StateFullGroupPage());
//布局组件示例
//void main() => runApp(LayoutDemoPage());
class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: Scaffold(
        appBar: AppBar(title: Text('Flutter路由跳转示例')),
        body: RouteNavigator(),
      ),
      routes: <String,WidgetBuilder>{
        'less': (BuildContext context) => LessGroupPage(),
        'ful': (BuildContext context) => StateFullGroupPage(),
        'layout': (BuildContext context) => LayoutDemoPage()
      },
    );
  }
}

class RouteNavigator extends StatefulWidget {
  @override
  _RouteNavigatorState createState() => _RouteNavigatorState();
}

class _RouteNavigatorState extends State<RouteNavigator> {
  bool byName = false;

  @override
  Widget build(BuildContext context) {
    return Container(
      child: Column(
        children: <Widget>[
          SwitchListTile(
            value: byName,
            title: Text('${byName ? '' : '不'}通过路由名跳转'),
            onChanged: (value){
              setState(() {
                byName = value;
              });
            },
          ),
          _item('StatelessWidget与基础组件', LessGroupPage(), 'less'),
          _item('StatefulWidget与基础组件', StateFullGroupPage(), 'ful'),
          _item('Flutter 布局示例', LayoutDemoPage(), 'layout')
        ],
      ),
    );
  }

  _item(String label, Widget page, String pageKey) {
    return RaisedButton(
      onPressed: (){
          if(byName){
            Navigator.pushNamed(context, pageKey);
          }else{  //不需要在 MaterialApp 配置 routes 路由列表
            Navigator.push(context, MaterialPageRoute(builder:(context) => page));
          }
      },
      child: Text(label),
    );
  }
}
